package com.srxz.personnelsystem.system.service.mapimpl;

import com.srxz.personnelsystem.system.entity.Department;
import com.srxz.personnelsystem.system.entity.LoginInfo;
import com.srxz.personnelsystem.system.mapper.converter.DepartmentMapper;
import com.srxz.personnelsystem.system.mapper.converter.LoginInfoMapper;
import com.srxz.personnelsystem.system.mapper.dto.UserInfoDTO;
import com.srxz.personnelsystem.system.repository.DepartmentRepository;
import com.srxz.personnelsystem.system.repository.LoginInfoRepository;
import com.srxz.personnelsystem.system.repository.UserInfoRepository;
import com.srxz.personnelsystem.system.service.BaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName: UserMapperServiceImpl
 * @author: wgchao
 * @createTime: 2019/1/10 12:38 PM
 * @Description: TODO
 */
public class UserMapperServiceImpl extends BaseService<UserInfoDTO> {
    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private LoginInfoRepository loginInfoRepository;
    @Autowired
    private LoginInfoMapper loginInfoMapper;
    @Autowired
    private DepartmentRepository departmentRepository;
    @Autowired
    private DepartmentMapper departmentMapper;

    public UserMapperServiceImpl extendExtraInfoDTO(UserInfoDTO userInfoDTO){
        if(userInfoDTO.getBirth() != null){
            //计算年龄
            userInfoDTO.setAge(getAgeByBirth(userInfoDTO.getBirth()));
        }
        if(userInfoDTO.getEntryTime() != null){
            //计算入职工龄
            userInfoDTO.setSeniority(getDatePoor(new Date(), userInfoDTO.getEntryTime()));
        }
        if(userInfoDTO.getStatus() !=null){
            if(userInfoDTO.getStatus() == 0){
                userInfoDTO.setStatusName("试用期");
            }else if(userInfoDTO.getStatus() == 1){
                userInfoDTO.setStatusName("在职");
            }else if(userInfoDTO.getStatus() == -1){
                userInfoDTO.setStatusName("离职");
            }else{
                userInfoDTO.setStatusName("在职");
            }
        }else{
            userInfoDTO.setStatusName("在职");
        }
        return this;
    }

    public UserMapperServiceImpl extendDepartmentName(UserInfoDTO userInfoDTO){
        if(!StringUtils.isEmpty(userInfoDTO.getDepartmentId())){
            Department department = departmentRepository.getOne(userInfoDTO.getDepartmentId());
            if(department != null){
                setAttribute(userInfoDTO, "departmentName", department.getName());
            }
        }
        return this;
    }

    public UserMapperServiceImpl extendLoginInfoDTO(UserInfoDTO userInfoDTO){
        if(userInfoDTO.getLogin()){
            LoginInfo loginInfo = loginInfoRepository.findFirstByUserIdAndDeleteTimeIsNull(userInfoDTO.getId());
            if(loginInfo != null){
                userInfoDTO.setLoginInfoDTO(loginInfoMapper.dto(loginInfo));
            }
        }
        return this;
    }

    public int getAgeByBirth(Date birthDay) {
        int age = 0;
        Calendar cal = Calendar.getInstance();
        if (cal.before(birthDay)) { //出生日期晚于当前时间，无法计算
            return 0;
        }
        int yearNow = cal.get(Calendar.YEAR);  //当前年份
        int monthNow = cal.get(Calendar.MONTH);  //当前月份
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); //当前日期
        cal.setTime(birthDay);
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
        age = yearNow - yearBirth;   //计算整岁数
        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;//当前日期在生日之前，年龄减一
            } else {
                age--;//当前月份在生日之前，年龄减一
            }
        }
        return age;
    }

    public String getDatePoor(Date endDate, Date nowDate) {

        long nd = 1000 * 24 * 60 * 60;

        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;

        DecimalFormat df=new DecimalFormat("0.00");//设置保留位数

        return df.format((float)day/365);
    }


}
